package mwa.cs.service;
import java.io.IOException;
import java.io.PrintWriter;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;

import mwa.cs.dao.ZoneDAO;
import mwa.cs.database.Database;

/**
 * Servlet implementation class ZoneComponentAPISrvl
 */
public class ZoneComponentAPISrvl extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private int vnSiteId;
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ZoneComponentAPISrvl() {
        super();
        // TODO Auto-generated constructor stub
    }
    @SuppressWarnings("unchecked")
	public void processRequest(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
    	
    	request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		response.setContentType("text/JSON");
		
		try {
    	
			if (request.getSession().getAttribute("site_id") != null) {
				vnSiteId = Integer.parseInt(""+ request.getSession().getAttribute("site_id"));
			} else {
				vnSiteId = 1;
			}
    	
		}catch (NullPointerException ex){
			vnSiteId = 1;
		} 
		
		Database db = new Database();
		PrintWriter voOut = response.getWriter();
		String[] vaMessage = null, vaValue = null;
		
		ZoneDAO voZone = new ZoneDAO(db);
		List<Map<String, Object>> voResult = voZone.findZoneBySite(vnSiteId);
		
		vaValue = new String[voResult.size()];
		vaMessage = new String[voResult.size()];
		
		for (int i = 0; i < voResult.size(); i++) {
			Map<String, Object> voObject = voResult.get(i);
			vaValue[i] = ((BigDecimal) voObject.get("ZONE_ID")).toString();
			vaMessage[i] = (String) voObject.get("ZONE_NAME");
		}
		
		db.close();
		JSONObject voObject = new JSONObject();
		JSONArray voListMessages = new JSONArray();
		if (vaMessage != null) {
			for (int i = 0; i < vaMessage.length; i++) {
				voListMessages.add(vaMessage[i]);
			}
		}
		voObject.put("messages", voListMessages);
		JSONArray voListValues = new JSONArray();
		if (vaValue != null) {
			for (int i = 0; i < vaValue.length; i++) {
				voListValues.add(vaValue[i]);
			}
		}
		voObject.put("values", voListValues);
		voOut.print(voObject.toJSONString());
    	
    }
   
	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		processRequest(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		processRequest(request, response);
	}

}
